Methods for combining and mixing images.
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import imageio
from _utils import *
import warnings
warnings.filterwarnings('ignore')
Given the input images $A$ and $B$ (sRGB colorspace).
p.s.: All the input images are normalized between 0-1.
# Input and normalize to [0, 1]
A = imageio.imread('../_data/fruits.png')/255
histogram(A, bins=2**8, interval=[0, 1])
B = imageio.imread('../_data/pimentos.png')/255
histogram(B, bins=2**8, interval=[0, 1])
n1, n2, _ = A.shape
a = np.zeros((n1, n2, 1))
a[50:350, 50:350] = 1
n1, n2, _ = B.shape
b = np.zeros((n1, n2, 1))
b[200:400] = 1
panel(
np.array([a[...,0], b[...,0]]), [2, 1],
text_size=24, text_color='lightgreen',
texts=[r'$\alpha$', r'$\beta$']
)
O = A*a + B*(1 - a)
histogram(O, bins=2**8, interval=[0, 1])
O = A*b + B*(1 - a)
histogram(O, bins=2**8, interval=[0, 1])
O = A + B*(1 - a)/b
cond = (a > b)[...,0]
O[cond] = A[cond]
histogram(O, bins=2**8, interval=[0, 1])
O = A + B*(1 - a)/b
cond = ((a + b) < 1)[...,0]
O[cond] = (A + B)[cond]
histogram(O, bins=2**8, interval=[0, 1])
O = A*b
histogram(O, bins=2**8, interval=[0, 1])
O = A*(1 - b) + B*(1 - a)
histogram(O, bins=2**8, interval=[0, 1])
Due the input images are in sRGB colorspace, it is necessary to linearize them by the $\gamma = 2.2$ before operating and the output must be transformed to sRGB by inverting the $\gamma$.
$$ \large I' = I^{\gamma} \quad ; \quad O' = O^{\frac{1}{\gamma}} $$# Gamma
g = 2.2
# Gamma corrected
A_, B_ = A**g, B**g
O = (A_ + B_)/2
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = 1 - (1 - B_)/A_
# Deal with zero division
O[O < 0] = 0
O[O > 1] = 1
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = np.absolute(A_ - B_)
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = A_/B_
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = A_ + B_ - 2*A_*B_
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = B_ - A_
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = 2*A_*B_/(A_ + B_)
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = (A_**2 + B_**2)**0.5
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = np.maximum(A_, B_)
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = np.minimum(A_, B_)
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = A_ - B_
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = A_*B_
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = A_ + B_
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = A_ + B_ - A_*B_
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = B_*(2*A_ + B_*(1 - A_*B_))
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = A_ + B_ - A_*B_
cond = A_ < 0.5
O[cond] = (A_*B_)[cond]
histogram(O**(1/g), bins=2**8, interval=[0, 1])
O = A_ + B_ - A_*B_
cond = B_ < 0.5
O[cond] = (A_*B_)[cond]
histogram(O**(1/g), bins=2**8, interval=[0, 1])